
'''	
	This file is part of Arendal.

	Copyright (C) 2008	Mohamed Hazem <mohamed.hazem@student.guc.edu.eg>


	Arendal is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	Arendal is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with Arendal.	If not, see <http://www.gnu.org/licenses/>.
'''


from PyQt4.QtCore import QVariant, Qt, SIGNAL, QString, QStringList, QModelIndex
from PyQt4.QtSql import QSqlDatabase, QSqlQuery, QSqlRelationalDelegate, QSqlTableModel, QSqlRecord, QSqlRelationalTableModel, QSqlRelation
from PyQt4.QtGui import QWidget, QApplication, QListWidget, QCompleter, QLineEdit, QComboBox, QHBoxLayout, QVBoxLayout, QLabel, QPushButton, QMessageBox, QGridLayout, QComboBox, QWizard, QRadioButton,QGroupBox, QWizardPage, QPixmap, QDateTimeEdit, QDataWidgetMapper, QSpinBox, QCheckBox, QListWidget, QTableView


class eventSelectionWizardPage(QWizardPage):
	def __init__(self, userID):
		QWizardPage.__init__(self)
		self.userID = userID

	def initializePage(self):
		print "init userid: " + str(self.userID)
		self.setTitle("Event Type")

		eventsGroup = QGroupBox("Select Event Type")

		self.outingEvent = QRadioButton("Outing")
		self.outingEvent.setChecked(True)
		self.birthdayEvent = QRadioButton("Birthday Party")
		self.businessMeetingEvent = QRadioButton("Business Meeting")
		self.sportsEvent = QRadioButton("Sports Event")
		self.tripEvent = QRadioButton("Trip")

		radioButtonsLayout = QVBoxLayout()
		radioButtonsLayout.addWidget(self.outingEvent)
		radioButtonsLayout.addWidget(self.birthdayEvent)
		radioButtonsLayout.addWidget(self.businessMeetingEvent)
		radioButtonsLayout.addWidget(self.sportsEvent)
		radioButtonsLayout.addWidget(self.tripEvent)

		eventsGroup.setLayout(radioButtonsLayout)


		grid = QGridLayout()
		grid.addWidget(eventsGroup)
		self.setLayout(grid)

	def nextId(self):
		if self.outingEvent.isChecked():
			return 2
		elif self.birthdayEvent.isChecked():
			return 3
		elif self.businessMeetingEvent.isChecked():
			return 4
		elif self.sportsEvent.isChecked():
			return 5
		elif self.tripEvent.isChecked():
			return 6




class outingEventWizardPage(QWizardPage):
	def __init__(self, userID):
		QWizardPage.__init__(self)
		self.userID = userID

	def initializePage(self):
		self.setTitle("Outing Event")
		self.setPixmap(QWizard.BackgroundPixmap, QPixmap("images/outing.png"))


		self.titleLabel = QLabel("Title:")
		self.locationLabel = QLabel("Location:")
		self.startDateLabel = QLabel("Start Date:")
		self.startTimeLabel = QLabel("Start Time:")
		self.endDateLabel = QLabel("End Date:")
		self.endTimeLabel = QLabel("End Time:")
		self.typeLabel = QLabel("Type:")
		self.descriptionLabel = QLabel("Description:")
		self.meetingPointLabel = QLabel("Meeting Point:")
		self.inviteLabel = QLabel("Invite:")
		

		self.eventidLineEdit = QLineEdit()
		self.eventCreatorLineEdit = QLineEdit()
		self.titleLineEdit = QLineEdit()
		self.locationLineEdit = QLineEdit()
		self.startDateLineEdit = QDateTimeEdit()
		self.startDateLineEdit.setCalendarPopup(True)
		self.startDateLineEdit.setDisplayFormat("dd-MM-yyyy")
		self.startTimeLineEdit = QDateTimeEdit()
		self.startTimeLineEdit.setDisplayFormat("hh:mm AP")
		self.endDateLineEdit = QDateTimeEdit()
		self.endDateLineEdit.setCalendarPopup(True)
		self.endDateLineEdit.setDisplayFormat("dd-MM-yyyy")
		self.endTimeLineEdit = QDateTimeEdit()
		self.endTimeLineEdit.setDisplayFormat("hh:mm AP")
		self.eventTypeLineEdit = QLineEdit()
		self.typeLineEdit = QLineEdit()
		self.descriptionLineEdit = QLineEdit()
		self.meetingPointLineEdit = QLineEdit()
		
		
#		self.completer.setCaseSensitivity(Qt.CaseInsensitive)
#		self.completer.setCompletionMode(QCompleter.PopupCompletion)
		modelCompletion = QSqlTableModel()
		modelCompletion.setTable("User")
		modelCompletion.select()
		i = 0
		
		completionList = QStringList()
#		completionList.append("tetsss")
		print completionList.count()
		print completionList[:]
		while (i < modelCompletion.rowCount() + 1):
#			print modelCompletion.record(i).value("username").toString()
			completionList.append(modelCompletion.record(i).value("username").toString())
			print completionList[i]
			i = i + 1
		self.completer = QCompleter(completionList)
		print completionList.count()
		print completionList.first()
		self.inviteLine = QLineEdit()
#		self.completer.setWidget(self.inviteLine)

		self.inviteLine.setCompleter(self.completer)
		self.inviteList = QListWidget()
		


		self.registerField("outingTitle*", self.titleLineEdit)
		self.registerField("outingLocation*", self.locationLineEdit)
		self.registerField("outingStartDate*", self.startDateLineEdit)
		self.registerField("outingStartTime*", self.startTimeLineEdit)
		self.registerField("outingMeetingPoint*", self.meetingPointLineEdit)
		self.registerField("invite*", self.inviteList)

		self.grid = QGridLayout()

		self.grid.addWidget(self.titleLabel, 1, 0)
		self.grid.addWidget(self.titleLineEdit, 1, 1)

		self.grid.addWidget(self.locationLabel, 2, 0)
		self.grid.addWidget(self.locationLineEdit, 2, 1)

		self.grid.addWidget(self.startDateLabel, 3, 0)
		self.grid.addWidget(self.startDateLineEdit, 3, 1)

		self.grid.addWidget(self.startTimeLabel, 4, 0)
		self.grid.addWidget(self.startTimeLineEdit, 4, 1)

		self.grid.addWidget(self.endDateLabel, 5, 0)
		self.grid.addWidget(self.endDateLineEdit, 5, 1)

		self.grid.addWidget(self.endTimeLabel, 6, 0)
		self.grid.addWidget(self.endTimeLineEdit, 6, 1)

		self.grid.addWidget(self.typeLabel, 7, 0)
		self.grid.addWidget(self.typeLineEdit, 7, 1)

		self.grid.addWidget(self.descriptionLabel, 8, 0)
		self.grid.addWidget(self.descriptionLineEdit, 8, 1)

		self.grid.addWidget(self.meetingPointLabel, 9, 0)
		self.grid.addWidget(self.meetingPointLineEdit, 9, 1)

		self.grid.addWidget(self.inviteLabel, 10, 0)
		self.grid.addWidget(self.inviteLine, 10, 1)
		self.grid.addWidget(self.inviteList, 11, 1)

		self.modelFake = QSqlTableModel(self)
		self.modelFake.setTable("Event")
		self.modelFake.select()
		self.modelFake.setEditStrategy(QSqlTableModel.OnManualSubmit)
		self.modelFake.insertRows(self.modelFake.rowCount(), 1)
		self.modelFake.setData(self.modelFake.index(self.modelFake.rowCount() - 1, 1), QVariant(self.userID))
		self.modelFake.submitAll()
		self.eventId = self.modelFake.record(self.modelFake.rowCount() - 1).value("eventID").toInt()[0] + 1
		self.modelFake.removeRows(self.modelFake.rowCount() - 1, 1)
		self.modelFake.submitAll()

		self.modelEvent = QSqlTableModel(self)
		self.connect(self.modelEvent, SIGNAL("beforeInsert (QSqlRecord&)"), self.doBeforeInsertion)
		self.modelEvent.setTable("Event")
		self.modelEvent.select()

		self.modelOuting = QSqlTableModel(self)
		self.connect(self.modelOuting, SIGNAL("beforeInsert (QSqlRecord&)"), self.doBeforeInsertion)
		self.modelOuting.setTable("Outing")
		self.modelOuting.select()

		self.modelAttends = QSqlTableModel(self)
		self.modelAttends.setEditStrategy(QSqlTableModel.OnManualSubmit)
		self.modelAttends.setTable("Attends")
		self.modelAttends.select()

		self.eventMapper = QDataWidgetMapper(self)
		self.eventMapper.setSubmitPolicy(QDataWidgetMapper.ManualSubmit)
		self.eventMapper.setModel(self.modelEvent)
#		self.eventMapper.addMapping(self.eventidLineEdit, 0)
		self.eventMapper.addMapping(self.eventCreatorLineEdit, 1)
		self.eventMapper.addMapping(self.titleLineEdit, 2)
		self.eventMapper.addMapping(self.locationLineEdit, 3)
		self.eventMapper.addMapping(self.startDateLineEdit, 4)
		self.eventMapper.addMapping(self.startTimeLineEdit, 5)
		self.eventMapper.addMapping(self.endDateLineEdit, 6)
		self.eventMapper.addMapping(self.endTimeLineEdit, 7)
		self.eventMapper.addMapping(self.eventTypeLineEdit, 8)

		self.outingMapper = QDataWidgetMapper(self)
		self.outingMapper.setSubmitPolicy(QDataWidgetMapper.ManualSubmit)
		self.outingMapper.setModel(self.modelOuting)
		self.outingMapper.addMapping(self.eventidLineEdit, 0)
		self.outingMapper.addMapping(self.typeLineEdit, 1)
		self.outingMapper.addMapping(self.descriptionLineEdit, 2)
		self.outingMapper.addMapping(self.meetingPointLineEdit, 3)

		self.hiddenInviteLine = QLineEdit()
		self.inviteMapper = QDataWidgetMapper(self)
		self.inviteMapper.setSubmitPolicy(QDataWidgetMapper.ManualSubmit)
		self.inviteMapper.setModel(self.modelAttends)
		self.inviteMapper.addMapping(self.eventidLineEdit, 0)
		self.inviteMapper.addMapping(self.hiddenInviteLine, 1)
		self.connect(self.inviteLine, SIGNAL("returnPressed()"), self.addToList)
		

		self.modelEvent.insertRows(self.modelEvent.rowCount(), 1)
		self.modelOuting.insertRows(self.modelOuting.rowCount(), 1)
		self.modelAttends.insertRows(self.modelAttends.rowCount(), 1)

		self.eventMapper.toLast()
		self.outingMapper.toLast()
		self.inviteMapper.toLast()

		self.setLayout(self.grid)
		self.show()

	def validatePage(self):
		self.eventCreatorLineEdit.setText("%s" % (self.userID))
		self.eventTypeLineEdit.setText("Outing")
		self.eventMapper.submit()
		self.modelEvent.select()
		eventId = self.modelEvent.record(self.modelEvent.rowCount() - 1).value("eventID").toInt()[0]
		self.eventidLineEdit.setText(str(eventId))
		print "Event error: " + self.modelEvent.lastError().text()
		print "Outing error: " + self.modelOuting.lastError().text()
		self.outingMapper.submit()
		if self.inviteList.count() == 0:
			return True
		else:
			self.modelAttends.submitAll()
			print "compan err: " + self.modelAttends.lastError().text()
			return True
		print self.modelAttends.lastError().text()

	def nextId(self):
		return -1

	def doBeforeInsertion(self, sqlRecord):
		for i in range(sqlRecord.count()):
			if sqlRecord.value(i).toString() == '':
				sqlRecord.setNull(i)
			else:
				pass

	def addToList(self):
		self.eventidLineEdit.setText(str(self.eventId))
		print self.eventId
		self.inviteList.addItem(self.inviteLine.text())
		self.modelHidden = QSqlTableModel()
		self.modelHidden.setTable("User")
		self.modelHidden.setFilter("username='%s'" % (self.inviteLine.text()))
		self.modelHidden.select()
		id = self.modelHidden.record(0).value(0).toString()
		print id
		self.hiddenInviteLine.setText(id)
		self.inviteMapper.submit()
		self.inviteLine.clear()
		self.modelAttends.insertRows(self.modelAttends.rowCount(), 1)
		self.inviteMapper.toLast()
		print self.modelAttends.rowCount()
#		self.vi = QTableView()
#		self.vi.setModel(self.modelHidden)
#		self.grid.addWidget(self.vi)



class birthdayEventWizardPage(QWizardPage):
	def __init__(self, userID):
		QWizardPage.__init__(self)
		self.userID = userID

	def initializePage(self):
		self.setTitle("Birthday Event")
		self.setPixmap(QWizard.BackgroundPixmap, QPixmap("images/birthday.png"))


		self.titleLabel = QLabel("Title:")
		self.locationLabel = QLabel("Location:")
		self.startDateLabel = QLabel("Start Date:")
		self.startTimeLabel = QLabel("Start Time:")
		self.endDateLabel = QLabel("End Date:")
		self.endTimeLabel = QLabel("End Time:")
		self.ownerLabel = QLabel("Birthday Owner:")
		self.minimumChargeLabel = QLabel("Minumum Charge")
		self.presentLabel = QLabel("Present:")
		self.surpriseLabel = QLabel("Surprise:")
		

		self.eventidLineEdit = QLineEdit()
		self.eventCreatorLineEdit = QLineEdit()
		self.titleLineEdit = QLineEdit()
		self.locationLineEdit = QLineEdit()
		self.startDateLineEdit = QDateTimeEdit()
		self.startDateLineEdit.setCalendarPopup(True)
		self.startDateLineEdit.setDisplayFormat("dd-MM-yyyy")
		self.startTimeLineEdit = QDateTimeEdit()
		self.startTimeLineEdit.setDisplayFormat("hh:mm AP")
		self.endDateLineEdit = QDateTimeEdit()
		self.endDateLineEdit.setCalendarPopup(True)
		self.endDateLineEdit.setDisplayFormat("dd-MM-yyyy")
		self.endTimeLineEdit = QDateTimeEdit()
		self.endTimeLineEdit.setDisplayFormat("hh:mm AP")
		self.eventTypeLineEdit = QLineEdit()
		self.ownerLineEdit = QLineEdit()
		self.minimumChargeLineEdit = QSpinBox()
		self.minimumChargeLineEdit.setMaximum(999999999)
		self.presentLineEdit = QLineEdit()
		self.surpriseLineEdit = QCheckBox()


		self.registerField("birthdayTitle*", self.titleLineEdit)
		self.registerField("birthdayLocation*", self.locationLineEdit)
		self.registerField("birthdayStartDate*", self.startDateLineEdit)
		self.registerField("birthdayStartTime*", self.startTimeLineEdit)
		self.registerField("birthdayOwner*", self.ownerLineEdit)

		grid = QGridLayout()

		grid.addWidget(self.titleLabel, 1, 0)
		grid.addWidget(self.titleLineEdit, 1, 1)

		grid.addWidget(self.locationLabel, 2, 0)
		grid.addWidget(self.locationLineEdit, 2, 1)

		grid.addWidget(self.startDateLabel, 3, 0)
		grid.addWidget(self.startDateLineEdit, 3, 1)

		grid.addWidget(self.startTimeLabel, 4, 0)
		grid.addWidget(self.startTimeLineEdit, 4, 1)

		grid.addWidget(self.endDateLabel, 5, 0)
		grid.addWidget(self.endDateLineEdit, 5, 1)

		grid.addWidget(self.endTimeLabel, 6, 0)
		grid.addWidget(self.endTimeLineEdit, 6, 1)

		grid.addWidget(self.ownerLabel, 7, 0)
		grid.addWidget(self.ownerLineEdit, 7, 1)

		grid.addWidget(self.minimumChargeLabel, 8, 0)
		grid.addWidget(self.minimumChargeLineEdit, 8, 1)

		grid.addWidget(self.presentLabel, 9, 0)
		grid.addWidget(self.presentLineEdit, 9, 1)

		grid.addWidget(self.surpriseLabel, 10, 0)
		grid.addWidget(self.surpriseLineEdit, 10, 1)


		self.modelEvent = QSqlTableModel(self)
		self.connect(self.modelEvent, SIGNAL("beforeInsert (QSqlRecord&)"), self.doBeforeInsertion)
		self.modelEvent.setTable("Event")
		self.modelEvent.select()

		self.modelBirthday = QSqlTableModel(self)
		self.connect(self.modelBirthday, SIGNAL("beforeInsert (QSqlRecord&)"), self.doBeforeInsertion)
		self.modelBirthday.setTable("Birthday")
		self.modelBirthday.select()

		self.eventMapper = QDataWidgetMapper(self)
		self.eventMapper.setSubmitPolicy(QDataWidgetMapper.ManualSubmit)
		self.eventMapper.setModel(self.modelEvent)
		self.eventMapper.addMapping(self.eventidLineEdit, 0)
		self.eventMapper.addMapping(self.eventCreatorLineEdit, 1)
		self.eventMapper.addMapping(self.titleLineEdit, 2)
		self.eventMapper.addMapping(self.locationLineEdit, 3)
		self.eventMapper.addMapping(self.startDateLineEdit, 4)
		self.eventMapper.addMapping(self.startTimeLineEdit, 5)
		self.eventMapper.addMapping(self.endDateLineEdit, 6)
		self.eventMapper.addMapping(self.endTimeLineEdit, 7)
		self.eventMapper.addMapping(self.eventTypeLineEdit, 8)

		self.birthdayMapper = QDataWidgetMapper(self)
		self.birthdayMapper.setSubmitPolicy(QDataWidgetMapper.ManualSubmit)
		self.birthdayMapper.setModel(self.modelBirthday)
		self.birthdayMapper.addMapping(self.eventidLineEdit, 0)
		self.birthdayMapper.addMapping(self.ownerLineEdit, 1)
		self.birthdayMapper.addMapping(self.minimumChargeLineEdit, 2)
		self.birthdayMapper.addMapping(self.presentLineEdit, 3)
		self.birthdayMapper.addMapping(self.surpriseLineEdit, 4)

		self.modelEvent.insertRows(self.modelEvent.rowCount(), 1)
		self.modelBirthday.insertRows(self.modelBirthday.rowCount(), 1)

		self.eventMapper.toLast()
		self.birthdayMapper.toLast()

		self.setLayout(grid)
		self.show()

	def validatePage(self):
		self.eventCreatorLineEdit.setText("%s" % (self.userID))
		self.eventTypeLineEdit.setText("Birthday")
		self.eventMapper.submit()
		self.modelEvent.select()
		eventId = self.modelEvent.record(self.modelEvent.rowCount() - 1).value("eventID").toInt()[0]
		self.eventidLineEdit.setText(str(eventId))
		print "Event error: " + self.modelEvent.lastError().text()
		print "Outing error: " + self.modelBirthday.lastError().text()
		if self.birthdayMapper.submit():
			return True

	def nextId(self):
		return -1

	def doBeforeInsertion(self, sqlRecord):
		for i in range(sqlRecord.count()):
			if sqlRecord.value(i).toString() == '':
				sqlRecord.setNull(i)
			else:
				pass




class sportsEventWizardPage(QWizardPage):
	def __init__(self, userID):
		QWizardPage.__init__(self)
		self.userID = userID

	def initializePage(self):
		self.setTitle("Sports Event")
		self.setPixmap(QWizard.BackgroundPixmap, QPixmap("images/sports.png"))


		self.titleLabel = QLabel("Title:")
		self.locationLabel = QLabel("Location:")
		self.startDateLabel = QLabel("Start Date:")
		self.startTimeLabel = QLabel("Start Time:")
		self.endDateLabel = QLabel("End Date:")
		self.endTimeLabel = QLabel("End Time:")
		self.typeLabel = QLabel("Type:")
		self.teamOneLabel = QLabel("Team 1:")
		self.teamTwoLabel = QLabel("Team 2:")
		

		self.eventidLineEdit = QLineEdit()
		self.eventCreatorLineEdit = QLineEdit()
		self.titleLineEdit = QLineEdit()
		self.locationLineEdit = QLineEdit()
		self.startDateLineEdit = QDateTimeEdit()
		self.startDateLineEdit.setCalendarPopup(True)
		self.startDateLineEdit.setDisplayFormat("dd-MM-yyyy")
		self.startTimeLineEdit = QDateTimeEdit()
		self.startTimeLineEdit.setDisplayFormat("hh:mm AP")
		self.endDateLineEdit = QDateTimeEdit()
		self.endDateLineEdit.setCalendarPopup(True)
		self.endDateLineEdit.setDisplayFormat("dd-MM-yyyy")
		self.endTimeLineEdit = QDateTimeEdit()
		self.endTimeLineEdit.setDisplayFormat("hh:mm AP")
		self.eventTypeLineEdit = QLineEdit()
		self.typeLineEdit = QLineEdit()
		self.teamOneLineEdit = QLineEdit()
		self.teamTwoLineEdit = QLineEdit()


		self.registerField("sportsTitle*", self.titleLineEdit)
		self.registerField("sportsLocation*", self.locationLineEdit)
		self.registerField("sportsStartDate*", self.startDateLineEdit)
		self.registerField("sportsStartTime*", self.startTimeLineEdit)
		self.registerField("sportsType*", self.typeLineEdit)
		self.registerField("sportsTeamOne*", self.teamOneLineEdit)
		self.registerField("sportsTeamTwo*", self.teamTwoLineEdit)

		grid = QGridLayout()

		grid.addWidget(self.titleLabel, 1, 0)
		grid.addWidget(self.titleLineEdit, 1, 1)

		grid.addWidget(self.locationLabel, 2, 0)
		grid.addWidget(self.locationLineEdit, 2, 1)

		grid.addWidget(self.startDateLabel, 3, 0)
		grid.addWidget(self.startDateLineEdit, 3, 1)

		grid.addWidget(self.startTimeLabel, 4, 0)
		grid.addWidget(self.startTimeLineEdit, 4, 1)

		grid.addWidget(self.endDateLabel, 5, 0)
		grid.addWidget(self.endDateLineEdit, 5, 1)

		grid.addWidget(self.endTimeLabel, 6, 0)
		grid.addWidget(self.endTimeLineEdit, 6, 1)

		grid.addWidget(self.typeLabel, 7, 0)
		grid.addWidget(self.typeLineEdit, 7, 1)

		grid.addWidget(self.teamOneLabel, 8, 0)
		grid.addWidget(self.teamOneLineEdit, 8, 1)

		grid.addWidget(self.teamTwoLabel, 9, 0)
		grid.addWidget(self.teamTwoLineEdit, 9, 1)


		self.modelEvent = QSqlTableModel(self)
		self.connect(self.modelEvent, SIGNAL("beforeInsert (QSqlRecord&)"), self.doBeforeInsertion)
		self.modelEvent.setTable("Event")
		self.modelEvent.select()

		self.modelSports = QSqlTableModel(self)
		self.connect(self.modelSports, SIGNAL("beforeInsert (QSqlRecord&)"), self.doBeforeInsertion)
		self.modelSports.setTable("SportsEvent")
		self.modelSports.select()

		self.eventMapper = QDataWidgetMapper(self)
		self.eventMapper.setSubmitPolicy(QDataWidgetMapper.ManualSubmit)
		self.eventMapper.setModel(self.modelEvent)
		self.eventMapper.addMapping(self.eventidLineEdit, 0)
		self.eventMapper.addMapping(self.eventCreatorLineEdit, 1)
		self.eventMapper.addMapping(self.titleLineEdit, 2)
		self.eventMapper.addMapping(self.locationLineEdit, 3)
		self.eventMapper.addMapping(self.startDateLineEdit, 4)
		self.eventMapper.addMapping(self.startTimeLineEdit, 5)
		self.eventMapper.addMapping(self.endDateLineEdit, 6)
		self.eventMapper.addMapping(self.endTimeLineEdit, 7)
		self.eventMapper.addMapping(self.eventTypeLineEdit, 8)

		self.sportsMapper = QDataWidgetMapper(self)
		self.sportsMapper.setSubmitPolicy(QDataWidgetMapper.ManualSubmit)
		self.sportsMapper.setModel(self.modelSports)
		self.sportsMapper.addMapping(self.eventidLineEdit, 0)
		self.sportsMapper.addMapping(self.typeLineEdit, 1)
		self.sportsMapper.addMapping(self.teamOneLineEdit, 2)
		self.sportsMapper.addMapping(self.teamTwoLineEdit, 3)

		self.modelEvent.insertRows(self.modelEvent.rowCount(), 1)
		self.modelSports.insertRows(self.modelSports.rowCount(), 1)

		self.eventMapper.toLast()
		self.sportsMapper.toLast()

		self.setLayout(grid)
		self.show()

	def validatePage(self):
		self.eventCreatorLineEdit.setText("%s" % (self.userID))
		self.eventTypeLineEdit.setText("SportsEvent")
		self.eventMapper.submit()
		self.modelEvent.select()
		eventId = self.modelEvent.record(self.modelEvent.rowCount() - 1).value("eventID").toInt()[0]
		self.eventidLineEdit.setText(str(eventId))
		print "Event error: " + self.modelEvent.lastError().text()
		print "Outing error: " + self.modelSports.lastError().text()
		if self.sportsMapper.submit():
			return True

	def nextId(self):
		return -1

	def doBeforeInsertion(self, sqlRecord):
		for i in range(sqlRecord.count()):
			if sqlRecord.value(i).toString() == '':
				sqlRecord.setNull(i)
			else:
				pass



class businessMeetingEventWizardPage(QWizardPage):
	def __init__(self, userID):
		QWizardPage.__init__(self)
		self.userID = userID

	def initializePage(self):
		self.setTitle("Business Meeting Event")
		self.setPixmap(QWizard.BackgroundPixmap, QPixmap("images/business.png"))


		self.titleLabel = QLabel("Title:")
		self.locationLabel = QLabel("Location:")
		self.startDateLabel = QLabel("Start Date:")
		self.startTimeLabel = QLabel("Start Time:")
		self.endDateLabel = QLabel("End Date:")
		self.endTimeLabel = QLabel("End Time:")
		self.topicLabel = QLabel("Topic:")
		self.companiesLabel = QLabel("Meeting Companies:")


		self.eventidLineEdit = QLineEdit()
		self.eventCreatorLineEdit = QLineEdit()
		self.titleLineEdit = QLineEdit()
		self.locationLineEdit = QLineEdit()
		self.startDateLineEdit = QDateTimeEdit()
		self.startDateLineEdit.setCalendarPopup(True)
		self.startDateLineEdit.setDisplayFormat("dd-MM-yyyy")
		self.startTimeLineEdit = QDateTimeEdit()
		self.startTimeLineEdit.setDisplayFormat("hh:mm AP")
		self.endDateLineEdit = QDateTimeEdit()
		self.endDateLineEdit.setCalendarPopup(True)
		self.endDateLineEdit.setDisplayFormat("dd-MM-yyyy")
		self.endTimeLineEdit = QDateTimeEdit()
		self.endTimeLineEdit.setDisplayFormat("hh:mm AP")
		self.eventTypeLineEdit = QLineEdit()
		self.topicLineEdit = QLineEdit()
		self.companiesLineEdit = QLineEdit()
		self.companiesLineEdit.setMaxLength(15)
		self.connect(self.companiesLineEdit, SIGNAL("returnPressed ()"), self.insertCompany)
		self.companiesList = QListWidget()


		self.registerField("businessMeetingTitle*", self.titleLineEdit)
		self.registerField("businessMeetingLocation*", self.locationLineEdit)
		self.registerField("businessMeetingStartDate*", self.startDateLineEdit)
		self.registerField("businessMeetingStartTime*", self.startTimeLineEdit)
		self.registerField("meetingCompanyName*", self.companiesList)

		grid = QGridLayout()

		grid.addWidget(self.titleLabel, 1, 0)
		grid.addWidget(self.titleLineEdit, 1, 1)

		grid.addWidget(self.locationLabel, 2, 0)
		grid.addWidget(self.locationLineEdit, 2, 1)

		grid.addWidget(self.startDateLabel, 3, 0)
		grid.addWidget(self.startDateLineEdit, 3, 1)

		grid.addWidget(self.startTimeLabel, 4, 0)
		grid.addWidget(self.startTimeLineEdit, 4, 1)

		grid.addWidget(self.endDateLabel, 5, 0)
		grid.addWidget(self.endDateLineEdit, 5, 1)

		grid.addWidget(self.endTimeLabel, 6, 0)
		grid.addWidget(self.endTimeLineEdit, 6, 1)

		grid.addWidget(self.topicLabel, 7, 0)
		grid.addWidget(self.topicLineEdit, 7, 1)

		grid.addWidget(self.companiesLabel, 8, 0)
		grid.addWidget(self.companiesLineEdit, 8, 1)

		grid.addWidget(self.companiesList, 9, 1)


		self.modelEvent = QSqlTableModel(self)
		self.connect(self.modelEvent, SIGNAL("beforeInsert (QSqlRecord&)"), self.doBeforeInsertion)
		self.modelEvent.setTable("Event")
		self.modelEvent.select()

		self.modelBusiness = QSqlTableModel(self)
		self.connect(self.modelBusiness, SIGNAL("beforeInsert (QSqlRecord&)"), self.doBeforeInsertion)
		self.modelBusiness.setTable("BusinessMeeting")
		self.modelBusiness.select()

		self.modelCompanies = QSqlTableModel(self)
		self.connect(self.modelCompanies, SIGNAL("beforeInsert (QSqlRecord&)"), self.doBeforeInsertion)
		self.modelCompanies.setTable("MeetingCompanies")
		self.modelCompanies.select()
		self.modelCompanies.setEditStrategy(QSqlTableModel.OnManualSubmit)

		self.modelFake = QSqlTableModel(self)
		self.modelFake.setTable("Event")
		self.modelFake.select()
		self.modelFake.setEditStrategy(QSqlTableModel.OnManualSubmit)
		self.modelFake.insertRows(self.modelFake.rowCount(), 1)
		self.modelFake.setData(self.modelFake.index(self.modelFake.rowCount() - 1, 1), QVariant(self.userID))
		self.modelFake.submitAll()
		self.eventId = self.modelFake.record(self.modelFake.rowCount() - 1).value("eventID").toInt()[0] + 1
		self.modelFake.removeRows(self.modelFake.rowCount() - 1, 1)
		self.modelFake.submitAll()
		

		self.eventMapper = QDataWidgetMapper(self)
		self.eventMapper.setSubmitPolicy(QDataWidgetMapper.ManualSubmit)
		self.eventMapper.setModel(self.modelEvent)
		self.eventMapper.addMapping(self.eventidLineEdit, 0)
		self.eventMapper.addMapping(self.eventCreatorLineEdit, 1)
		self.eventMapper.addMapping(self.titleLineEdit, 2)
		self.eventMapper.addMapping(self.locationLineEdit, 3)
		self.eventMapper.addMapping(self.startDateLineEdit, 4)
		self.eventMapper.addMapping(self.startTimeLineEdit, 5)
		self.eventMapper.addMapping(self.endDateLineEdit, 6)
		self.eventMapper.addMapping(self.endTimeLineEdit, 7)
		self.eventMapper.addMapping(self.eventTypeLineEdit, 8)

		self.businessMapper = QDataWidgetMapper(self)
		self.businessMapper.setSubmitPolicy(QDataWidgetMapper.ManualSubmit)
		self.businessMapper.setModel(self.modelBusiness)
		self.businessMapper.addMapping(self.eventidLineEdit, 0)
		self.businessMapper.addMapping(self.topicLineEdit, 1)

		self.companiesMapper = QDataWidgetMapper(self)
		self.companiesMapper.setSubmitPolicy(QDataWidgetMapper.ManualSubmit)
		self.companiesMapper.setModel(self.modelCompanies)
		self.companiesMapper.addMapping(self.eventidLineEdit, 0)
		self.companiesMapper.addMapping(self.companiesLineEdit, 1)


		self.modelEvent.insertRows(self.modelEvent.rowCount(), 1)
		self.modelBusiness.insertRows(self.modelBusiness.rowCount(), 1)
		self.modelCompanies.insertRows(self.modelCompanies.rowCount(), 1)

		self.eventMapper.toLast()
		self.businessMapper.toLast()
		self.companiesMapper.toLast()

		self.setLayout(grid)
		self.show()

	def validatePage(self):
		self.eventCreatorLineEdit.setText("%s" % (self.userID))
		self.eventTypeLineEdit.setText("BusinessMeeting")
		self.eventMapper.submit()
		self.modelEvent.select()
		self.eventidLineEdit.setText(str(self.eventId))
		self.businessMapper.submit()
		if self.companiesList.count() == 0:
			return True
		else:
			self.modelCompanies.submitAll()
#			print "compan err: " + self.modelCompanies.lastError().text()
			return True

	def nextId(self):
		return -1

	def doBeforeInsertion(self, sqlRecord):
		for i in range(sqlRecord.count()):
			if sqlRecord.value(i).toString() == '':
				sqlRecord.setNull(i)
			else:
				pass

	def insertCompany(self):
		self.eventidLineEdit.setText(str(self.eventId))
		print "id: " + str(self.eventId)
		if self.companiesLineEdit.text() != '':
			self.companiesList.addItem(self.companiesLineEdit.text())
			self.companiesMapper.submit()
			self.modelCompanies.insertRows(self.modelCompanies.rowCount(), 1)
			self.companiesMapper.toLast()
			self.companiesLineEdit.clear()




class tripEventWizardPage(QWizardPage):
	def __init__(self, userID):
		QWizardPage.__init__(self)
		self.userID = userID

	def initializePage(self):
		self.setTitle("Trip Event")
		self.setPixmap(QWizard.BackgroundPixmap, QPixmap("images/trip.png"))


		self.titleLabel = QLabel("Title:")
		self.locationLabel = QLabel("Location:")
		self.startDateLabel = QLabel("Start Date:")
		self.startTimeLabel = QLabel("Start Time:")
		self.endDateLabel = QLabel("End Date:")
		self.endTimeLabel = QLabel("End Time:")
		self.transportationTypeLabel = QLabel("Transportation Type:")
		self.transportationAvailabilityLabel = QLabel("Transportation Availability:")
		self.gatheringPointLabel = QLabel("Gathering Point:")
		self.gatheringTimeLabel = QLabel("Gathering Time:")
		self.equipmentLabel = QLabel("Trip Equipment:")


		self.eventidLineEdit = QLineEdit()
		self.eventCreatorLineEdit = QLineEdit()
		self.titleLineEdit = QLineEdit()
		self.locationLineEdit = QLineEdit()
		self.startDateLineEdit = QDateTimeEdit()
		self.startDateLineEdit.setCalendarPopup(True)
		self.startDateLineEdit.setDisplayFormat("dd-MM-yyyy")
		self.startTimeLineEdit = QDateTimeEdit()
		self.startTimeLineEdit.setDisplayFormat("hh:mm AP")
		self.endDateLineEdit = QDateTimeEdit()
		self.endDateLineEdit.setCalendarPopup(True)
		self.endDateLineEdit.setDisplayFormat("dd-MM-yyyy")
		self.endTimeLineEdit = QDateTimeEdit()
		self.endTimeLineEdit.setDisplayFormat("hh:mm AP")
		self.eventTypeLineEdit = QLineEdit()
		self.transportationTypeLineEdit = QLineEdit()
		self.transportationAvailabilityLineEdit = QCheckBox()
		self.gatheringPointLineEdit = QLineEdit()
		self.gatheringTimeLineEdit = QDateTimeEdit()
		self.gatheringTimeLineEdit.setDisplayFormat("hh:mm AP")
		self.equipmentLineEdit = QLineEdit()
		self.equipmentLineEdit.setMaxLength(15)
		self.connect(self.equipmentLineEdit, SIGNAL("returnPressed ()"), self.insertEquipment)
		self.equipmentList = QListWidget()


		self.registerField("tripTitle*", self.titleLineEdit)
		self.registerField("tripLocation*", self.locationLineEdit)
		self.registerField("tripStartDate*", self.startDateLineEdit)
		self.registerField("tripStartTime*", self.startTimeLineEdit)
		self.registerField("gatheringPoint*", self.gatheringPointLineEdit)
		self.registerField("gatheringTime*", self.gatheringTimeLineEdit)
		self.registerField("tripEquipment*", self.equipmentList)

		grid = QGridLayout()

		grid.addWidget(self.titleLabel, 1, 0)
		grid.addWidget(self.titleLineEdit, 1, 1)

		grid.addWidget(self.locationLabel, 2, 0)
		grid.addWidget(self.locationLineEdit, 2, 1)

		grid.addWidget(self.startDateLabel, 3, 0)
		grid.addWidget(self.startDateLineEdit, 3, 1)

		grid.addWidget(self.startTimeLabel, 4, 0)
		grid.addWidget(self.startTimeLineEdit, 4, 1)

		grid.addWidget(self.endDateLabel, 5, 0)
		grid.addWidget(self.endDateLineEdit, 5, 1)

		grid.addWidget(self.endTimeLabel, 6, 0)
		grid.addWidget(self.endTimeLineEdit, 6, 1)

		grid.addWidget(self.transportationTypeLabel, 7, 0)
		grid.addWidget(self.transportationTypeLineEdit, 7, 1)

		grid.addWidget(self.transportationAvailabilityLabel, 8, 0)
		grid.addWidget(self.transportationAvailabilityLineEdit, 8, 1)

		grid.addWidget(self.gatheringPointLabel, 9, 0)
		grid.addWidget(self.gatheringPointLineEdit, 9, 1)

		grid.addWidget(self.gatheringTimeLabel, 10, 0)
		grid.addWidget(self.gatheringTimeLineEdit, 10, 1)

		grid.addWidget(self.equipmentLabel, 11, 0)
		grid.addWidget(self.equipmentLineEdit, 11, 1)

		grid.addWidget(self.equipmentList, 12, 1)


		self.modelEvent = QSqlTableModel(self)
		self.connect(self.modelEvent, SIGNAL("beforeInsert (QSqlRecord&)"), self.doBeforeInsertion)
		self.modelEvent.setTable("Event")
		self.modelEvent.select()

		self.modelTrip = QSqlTableModel(self)
		self.connect(self.modelTrip, SIGNAL("beforeInsert (QSqlRecord&)"), self.doBeforeInsertion)
		self.modelTrip.setTable("Trip")
		self.modelTrip.select()

		self.modelEquipment = QSqlTableModel(self)
		self.connect(self.modelEquipment, SIGNAL("beforeInsert (QSqlRecord&)"), self.doBeforeInsertion)
		self.modelEquipment.setTable("TripEquipment")
		self.modelEquipment.select()
		self.modelEquipment.setEditStrategy(QSqlTableModel.OnManualSubmit)

		self.modelFake = QSqlTableModel(self)
		self.modelFake.setTable("Event")
		self.modelFake.select()
		self.modelFake.setEditStrategy(QSqlTableModel.OnManualSubmit)
		self.modelFake.insertRows(self.modelFake.rowCount(), 1)
		self.modelFake.setData(self.modelFake.index(self.modelFake.rowCount() - 1, 1), QVariant(self.userID))
		self.modelFake.submitAll()
		self.eventId = self.modelFake.record(self.modelFake.rowCount() - 1).value("eventID").toInt()[0] + 1
		self.modelFake.removeRows(self.modelFake.rowCount() - 1, 1)
		self.modelFake.submitAll()
		

		self.eventMapper = QDataWidgetMapper(self)
		self.eventMapper.setSubmitPolicy(QDataWidgetMapper.ManualSubmit)
		self.eventMapper.setModel(self.modelEvent)
		self.eventMapper.addMapping(self.eventidLineEdit, 0)
		self.eventMapper.addMapping(self.eventCreatorLineEdit, 1)
		self.eventMapper.addMapping(self.titleLineEdit, 2)
		self.eventMapper.addMapping(self.locationLineEdit, 3)
		self.eventMapper.addMapping(self.startDateLineEdit, 4)
		self.eventMapper.addMapping(self.startTimeLineEdit, 5)
		self.eventMapper.addMapping(self.endDateLineEdit, 6)
		self.eventMapper.addMapping(self.endTimeLineEdit, 7)
		self.eventMapper.addMapping(self.eventTypeLineEdit, 8)

		self.tripMapper = QDataWidgetMapper(self)
		self.tripMapper.setSubmitPolicy(QDataWidgetMapper.ManualSubmit)
		self.tripMapper.setModel(self.modelTrip)
		self.tripMapper.addMapping(self.eventidLineEdit, 0)
		self.tripMapper.addMapping(self.transportationTypeLineEdit, 1)
		self.tripMapper.addMapping(self.transportationAvailabilityLineEdit, 2)
		self.tripMapper.addMapping(self.gatheringPointLineEdit, 3)
		self.tripMapper.addMapping(self.gatheringTimeLineEdit, 4)

		self.equipmentMapper = QDataWidgetMapper(self)
		self.equipmentMapper.setSubmitPolicy(QDataWidgetMapper.ManualSubmit)
		self.equipmentMapper.setModel(self.modelEquipment)
		self.equipmentMapper.addMapping(self.eventidLineEdit, 0)
		self.equipmentMapper.addMapping(self.equipmentLineEdit, 1)


		self.modelEvent.insertRows(self.modelEvent.rowCount(), 1)
		self.modelTrip.insertRows(self.modelTrip.rowCount(), 1)
		self.modelEquipment.insertRows(self.modelEquipment.rowCount(), 1)

		self.eventMapper.toLast()
		self.tripMapper.toLast()
		self.equipmentMapper.toLast()

		self.setLayout(grid)
		self.show()

	def validatePage(self):
		self.eventCreatorLineEdit.setText("%s" % (self.userID))
		self.eventTypeLineEdit.setText("Trip")
		self.eventMapper.submit()
		self.modelEvent.select()
		self.eventidLineEdit.setText(str(self.eventId))
		self.tripMapper.submit()
		if self.equipmentList.count() == 0:
			return True
		else:
			self.modelEquipment.submitAll()
			return True

	def nextId(self):
		return -1

	def doBeforeInsertion(self, sqlRecord):
		for i in range(sqlRecord.count()):
			if sqlRecord.value(i).toString() == '':
				sqlRecord.setNull(i)
			else:
				pass

	def insertEquipment(self):
		self.eventidLineEdit.setText(str(self.eventId))
		if self.equipmentLineEdit.text() != '':
			self.equipmentList.addItem(self.equipmentLineEdit.text())
			self.equipmentMapper.submit()
			self.modelEquipment.insertRows(self.modelEquipment.rowCount(), 1)
			self.equipmentMapper.toLast()
			self.equipmentLineEdit.clear()
